
package com.zhuge_new.http;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.R;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.exception.HttpException;
import com.lzy.okgo.exception.StorageException;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.lzy.okgo.utils.OkLogger;
import com.maning.mndialoglibrary.MProgressDialog;
import com.maning.mndialoglibrary.MToast;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

public abstract class StringDialogCallback extends StringCallback {

    private MProgressDialog mMProgressDialog;
    private Activity activity;
    private boolean isVisible = true;

    public StringDialogCallback(Activity activity) {
        this(activity, true);
    }

    public StringDialogCallback(Activity activity, boolean isVisible) {
        this.activity = activity;
        this.isVisible = isVisible;

        mMProgressDialog = new MProgressDialog.Builder(activity)
                .setCancelable(true)
                .isCanceledOnTouchOutside(false)
                .setDimAmount(0.5f)
                .build();
    }

    @Override
    public void onStart(Request<String, ? extends Request> request) {
        if (isVisible) mMProgressDialog.show();
    }

    @Override
    public void onSuccess(Response<String> response) {
        OkLogger.i(response.body());

        try {
            JSONObject obj = new JSONObject(response.body());

            String msgCode = obj.getString("msgcode");
            String msg = obj.isNull("msg") ? "请求成功！" : obj.getString("msg");

            if (!TextUtils.equals("100", msgCode)) {
                MToast.makeTextShort(activity, msg).show();
                onSuccessResponseErrorCode(response, msg, msgCode);
            } else {
                onSuccessResponse(response, msg, msgCode);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public abstract void onSuccessResponse(Response<String> response, String msg, String msgCode);

    public void onSuccessResponseErrorCode(Response<String> response, String msg, String msgCode) {
    }

    /**
     * 当缓存读取成功后，回调该方法
     */
    @Override
    public void onCacheSuccess(Response<String> response) {
        try {
            JSONObject obj = new JSONObject(response.body());

            String msgCode = new JSONObject(response.body()).getString("msgcode");

            if(msgCode.equals("102")){
                Toast.makeText(activity,"请重新登录",Toast.LENGTH_SHORT).show();
                clearData();
                try {
                    Intent intent = new Intent();
                    intent.setAction("Login_Action");
                    intent.addCategory("Login_Category");
                    activity.startActivity(intent);
                }catch (Exception e){
                    System.out.print("");
                }
                return;
            }



            String msg = obj.isNull("msg") ? "请求成功！" : obj.getString("msg");
            if (TextUtils.equals("100", msgCode)) onSuccessResponse(response, msg, msgCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFinish() {

        mMProgressDialog.dismiss();
    }

    @Override
    public void onError(Response<String> response) {
        super.onError(response);




        Throwable exception = response.getException();
        if (exception instanceof UnknownHostException || exception instanceof ConnectException) {
            MToast.makeTextShort(activity, "网络连接失败，请连接网络！").show();
        } else if (exception instanceof SocketTimeoutException) {
            MToast.makeTextShort(activity, "网络请求超时！").show();
        } else if (exception instanceof HttpException) {
            MToast.makeTextShort(activity, "服务器发生未知错误！").show();
        } else if (exception instanceof StorageException) {
            MToast.makeTextShort(activity, "SD卡不存在或没有权限！").show();
        } else {
            MToast.makeTextShort(activity, "网络数据请求失败！").show();
        }
    }


    private void clearData() {
        SharedPreferences sp=activity.getSharedPreferences("TrineaAndroidCommon", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putBoolean("isLogin", false);
        editor.putString("token", "");
        editor.putString("rongtoken", "");

        editor.putString("balanceSum", "0.00");
        editor.putString("profitSum", "0.00");
        editor.putString("withdrawSum", "0.00");
        editor.putString("levelName", "");
        editor.putString("platform", "");

        editor.putString("age", "");
        editor.putString("isPass", "");
        editor.putString("nickName", "");
        editor.putString("sex", "");
        editor.putString("userhead", "");
        editor.commit();

//        ACache.get(baseContext).put("isUpdating", false)
//
//        JPushInterface.stopPush(applicationContext)
//        JPushInterface.clearAllNotifications(applicationContext)
//
//        //清除通知栏消息
//        RongCloudContext.getInstance().clearNotificationMessage()
//        RongPushClient.clearAllPushNotifications(applicationContext)
//        RongIM.getInstance().logout()
    }


}
